package org.dromara.system.service.impl;

import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.XcxFollowBo;
import org.dromara.system.domain.vo.XcxFollowVo;
import org.dromara.system.domain.XcxFollow;
import org.dromara.system.mapper.XcxFollowMapper;
import org.dromara.system.service.IXcxFollowService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 我的关注Service业务层处理
 *
 * @author Lion Li
 * @date 2024-05-08
 */
@RequiredArgsConstructor
@Service
public class XcxFollowServiceImpl implements IXcxFollowService {

    private final XcxFollowMapper baseMapper;

    /**
     * 查询我的关注
     */
    @Override
    public XcxFollowVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询我的关注列表
     */
    @Override
    public TableDataInfo<XcxFollowVo> queryPageList(XcxFollowBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<XcxFollow> lqw = buildQueryWrapper(bo);
        Page<XcxFollowVo> result = baseMapper.selectVoPageZdy(bo.getItemName(),pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询我的关注列表
     */
    @Override
    public List<XcxFollowVo> queryList(XcxFollowBo bo) {
        LambdaQueryWrapper<XcxFollow> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<XcxFollow> buildQueryWrapper(XcxFollowBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<XcxFollow> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getFollowId() != null, XcxFollow::getFollowId, bo.getFollowId());
        lqw.eq(bo.getCreateBy() != null, XcxFollow::getCreateBy, bo.getCreateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getFollowType()), XcxFollow::getFollowType, bo.getFollowType());
        return lqw;
    }

    /**
     * 新增我的关注
     */
    @Override
    public Boolean insertByBo(XcxFollowBo bo) {
        XcxFollow add = MapstructUtils.convert(bo, XcxFollow.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改我的关注
     */
    @Override
    public Boolean updateByBo(XcxFollowBo bo) {
        XcxFollow update = MapstructUtils.convert(bo, XcxFollow.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(XcxFollow entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除我的关注
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<XcxFollowVo> queryPageListXcx(XcxFollowBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<XcxFollow> lqw = buildQueryWrapper(bo);
        Page<XcxFollowVo> result = baseMapper.selectVoPageZdy(bo.getItemName(),pageQuery.build(), lqw);
        for (XcxFollowVo xcxFollowVo : result.getRecords()){
            if("01".equals(xcxFollowVo.getFollowType())){
                xcxFollowVo.setTitle(xcxFollowVo.getWbItemInfoVo().getItemName());
                xcxFollowVo.setPushDate(xcxFollowVo.getWbItemInfoVo().getPublishTime());
            }else if("03".equals(xcxFollowVo.getFollowType())){
                xcxFollowVo.setTitle(xcxFollowVo.getWbMinuteBidBagVo().getItemName());
                xcxFollowVo.setPushDate(xcxFollowVo.getWbMinuteBidBagVo().getBidDate());
            }else if("04".equals(xcxFollowVo.getFollowType())){
                xcxFollowVo.setTitle(xcxFollowVo.getWbCandidateInfoVo().getHeadline());
                if(xcxFollowVo.getWbCandidateInfoVo().getPublicityTime() != null){
                    xcxFollowVo.setPushDate(DateUtils.dateTime(xcxFollowVo.getWbCandidateInfoVo().getPublicityTime()));
                }

            }
        }
        return TableDataInfo.build(result);
    }
}
